var util = require('../../utils/util.js')
var app = getApp();
var md5 = require('../md5.js');
Page({
  data: {
    biaodan: {

    }
  },
  submit: function () {
    this.setData({
      showModal: true
    })
  },
  onPullDownRefresh: function () {
    wx.stopPullDownRefresh({
      complete: (res) => { },
    });
  },
  inputChange(e) {
    var that = this;
    // console.log(e)
    var biaodan = that.data.biaodan;
    biaodan[e.currentTarget.dataset.name] = e.detail.value;
    that.setData({
      biaodan: biaodan
    });
    console.log(biaodan)
  },
  preventTouchMove: function () {

  },
  signInBtn(e) {
    var that = this;
    var verificationcode = that.data.verificationcode;
    console.log("curverificationcode=", verificationcode)
    //获取页面手机后四位
    var em = that.data.biaodan;
    var mobnumone = em.mobnumone
    var mobnumtwo = em.mobnumtwo
    var mobnumthree = em.mobnumthree
    var mobnumfour = em.mobnumfour
    console.log("mobnumone=", mobnumone)
    console.log("mobnumtwo=", mobnumtwo)
    console.log("mobnumthree=", mobnumthree)
    console.log("mobnumfour=", mobnumfour)
    if ((!(!mobnumone || mobnumone.trim() == "")) && (!(!mobnumtwo || mobnumtwo.trim() == "")) && (!(!mobnumthree || mobnumthree.trim() == ""))
      && (!(!mobnumfour || mobnumfour.trim() == ""))) {
      var mobnum = mobnumone + '' + mobnumtwo + '' + mobnumthree + '' + mobnumfour
      console.log("mobnum=", mobnum)
      //调用验证接口通过返回信息进行验证
      //调用pico接口,缓存
      var timestamp = Date.parse(new Date());
      timestamp = timestamp / 1000;
      var url = app.globalData.picoaddress + '/Attendee/user/scanverification'
      var k = {}
      k["v"] = '1.0.0'
      k["client"] = 'ios'
      k["timestamp"] = timestamp
      k["lan"] = 'cn'
      k["verificationcode"] = verificationcode
      var sign = app.globalData.picotoken + 'clientiostimestamp' + timestamp + 'verificationcode' + verificationcode + app.globalData.picotoken
      var curb64 = md5.hexMD5(sign)
      k["sign"] = curb64
      wx.request({
        header: {
          'content-type': 'application/x-www-form-urlencoded',
        },
        url: url,
        method: 'post',
        data: k,
        success(res) {
          console.log(mobnum)
          var mesg = res.data
          var curdata = mesg.data
          var mobileno = curdata.mobileno
          //截取手机号后四位
          var curmobileno = mobileno.substring(7, 11);
          if (mobnum == curmobileno){
             //登陆缓存id
            //调用pico接口,缓存
            var curtimestamp = Date.parse(new Date());
            curtimestamp = curtimestamp / 1000;
            var url = app.globalData.picoaddress + '/Attendee/user/scanlogin'
            var m = {}
            m["v"] = '1.0.0'
            m["client"] = 'ios'
            m["timestamp"] = curtimestamp
            m["lan"] = 'cn'
            m["mobileno"] = mobileno
            m["verificationcode"] = verificationcode
            var sign = app.globalData.picotoken + 'clientios' + 'mobileno' + mobileno +'timestamp' + curtimestamp +  'verificationcode' + verificationcode + app.globalData.picotoken
            var curb64 = md5.hexMD5(sign)
            m["sign"] = curb64
            wx.request({
              header: {
                'content-type': 'application/x-www-form-urlencoded',
              },
              url: url,
              method: 'post',
              data: m,
              success(res) {
                console.log(res)
                var mesg = res.data
                var curdata = mesg.data
                //缓存展会id，用户id
                var exhibitionid = curdata.exhibitionid
                var u = curdata.u
                wx.setStorageSync('exhibitionid', exhibitionid);
                wx.setStorageSync('u', u);
                wx.redirectTo({
                  url: '../aiIndex/aiIndex',
                })
              }, fail: res => {
                wx.redirectTo({
                  url: '../aiIndex/aiIndex',
                })
                console.log(res, "error")
              }
            }) 
          }
        }, fail: res => {
          wx.redirectTo({
            url: '../aiIndex/aiIndex',
          })
          console.log(res, "error")
        }
      })
      console.log("登录！！！")

    }else{
      wx.showToast({
        title: '请输入完整手机后四位！',
        icon: 'success',
        duration: 2000
      })
    }
  },

  go: function () {
    this.setData({
      showModal: false
    })
  },
  onLoad: function (options) {
    var that = this
    // 页面初始化 options为页面跳转所带来的参数
    var verificationcode = options.verificationcode
    console.log("verificationcode=", verificationcode)
    that.setData({
      verificationcode: verificationcode
    })
  },
})